﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;

namespace LUC.Domains
{
	public class DATA
	{
		public static DefNotifiesSet def_n_set;
		public static DefFeedsSet def_f_set;
		public static MainSet main_set;

		public static List<Feed> feeds { get; set; }

		public static void LoadDefSet()
		{
			try
			{
				DefaultSettings def_set = Serializator.FromFileToModel<DefaultSettings>
						(Program.luc_set.localset_name);
				def_n_set = def_set.def_n_set;
				def_f_set = def_set.def_f_set;
				main_set = def_set.main_set;
			}
			catch
			{
				DateTime hhmmss = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day,
					0, 0, 0);
				def_f_set = new DefFeedsSet();
				def_f_set.color = ColorTranslator.ToWin32(Color.Aqua);
				def_f_set.font = new SerializableFont(new Font("Arial", 12));
				def_f_set.period = 10;

				def_n_set = new DefNotifiesSet();
				def_n_set.color = ColorTranslator.ToWin32(Color.Black);
				def_n_set.delta_days = 0;
				def_n_set.delta_hhmmss = hhmmss;
				def_n_set.font = new SerializableFont(new Font("Arial", 12));
				def_n_set.is_delete = false;
				def_n_set.is_ringed = true;
				def_n_set.inc_days = 0;
				def_n_set.inc_hhmmss = hhmmss;
				def_n_set.time_over_color = ColorTranslator.ToWin32(Color.Red);
				def_n_set.time_over_font = new SerializableFont(new Font("Arial", 18));
				
				main_set = new MainSet();
				main_set.main_color = ColorTranslator.ToWin32(Color.Aquamarine);
				
				SaveDefSet();
			}
		}
		public static void SaveDefSet()
		{
			DefaultSettings def_set = new DefaultSettings(def_f_set, def_n_set, main_set);
			SetAutostart(main_set.Autorun);
			def_set.ModelToFile(Program.luc_set.localset_name);
		}

		static bool SetAutostart(bool autostart)
		{
			using (Microsoft.Win32.RegistryKey rKeyInit = Microsoft.Win32.Registry.CurrentUser)
			{
				Microsoft.Win32.RegistryKey rk =
					rKeyInit.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run", true);
				if (autostart)
				{
					try
					{
						rk.SetValue(Application.ProductName, Application.ExecutablePath);
					}
					catch
					{
						return false;
					}
				}
				else
				{
					try
					{
						rk.DeleteValue(Application.ProductName);
					}
					catch
					{
						//if can't delete regkey, then key is not found => there is no mistake! 
						return true;
					}
				}
				rk.Close();
			}
			return true;
		}
	


		public static void LoadFeeds()
		{
			try
			{
				feeds = Serializator.FromFileToModel<List<Feed>>(Program.luc_set.eventsset_name);
			}
			catch 
			{
				feeds = new List<Feed>();
				feeds.ModelToFile(Program.luc_set.eventsset_name);
			}
		}
		public static void SaveFeeds(BindingList<Feed> n_feeds)
		{
			feeds = new List<Feed>(n_feeds);
			feeds.ModelToFile<List<Feed>>(Program.luc_set.eventsset_name);
		}
		
	}
}
